﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;
using OMS.OMSService;

namespace OMS.Models
{
    public class UserModelView
    {
        public UserModelView() { }

        public UserModelView(UserModel userModel)
        {
            Id = userModel.Id;
            Login = userModel.Login;
            FirstName = userModel.FirstName;
            LastName = userModel.LastName;
            Email = userModel.Email;
            Role = userModel.Role;
            Region = userModel.Region;
            IsLocked = userModel.IsLocked;
        }

        public UserModel ToUserModel() 
        {
            UserModel userModel = new UserModel
            {
                Id = this.Id,
                IdSpecified = true,
                Login = this.Login,
                FirstName = this.FirstName,
                LastName = this.LastName,
                Email = this.Email,
                Role = this.Role,
                Password = this.Password,
                Region = this.Region
            };
            return userModel;
        }

        public int Id { get; set; }

        [Required]
        [StringLength(20, ErrorMessage = "Login is too long.", MinimumLength = 1)]
        [DataType(DataType.Text)]
        [RegularExpression(@"^\S+$", ErrorMessage = "Login cannot contain spaces!")]
        [Display(Name = "Login Name")]
        public string Login { get; set; }

        [Required]
        [StringLength(50, ErrorMessage = "First Name is too long.", MinimumLength = 1)]
        [DataType(DataType.Text)]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [Required]
        [StringLength(50, ErrorMessage = "Last Name is too long.", MinimumLength = 1)]
        [DataType(DataType.Text)]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }

        [Required]
        [Display(Name = "Password")]
        [DataType(DataType.Password)]
        [StringLength(10, ErrorMessage = "Password field cannot be shorter than {2} and longer than {1} characters", MinimumLength = 4)]
        [RegularExpression(@"^\S+$", ErrorMessage = "Password cannot contain spaces!")]
        public string Password { get; set; }

        [Required]
        [Display(Name = "Confirm Password")]
        [DataType(DataType.Password)]
        [Compare("Password", ErrorMessage = "Confirm Password is not equal to Password!")]
        public string ConfirmPassword { get; set; }

        [Required]
        [Display(Name = "E-mail")]
        [RegularExpression(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Incorrect format of Email Adress!")]
        [DataType(DataType.EmailAddress)]
        public string Email { get; set; }

        [Required]
        [Display(Name = "Region")]
        public string Region { get; set; }

        [Required]
        [Display(Name = "Role")]
        public string Role { get; set; }

        public bool IsLocked { get; private set; }
    }
}